package net.office.controller;

import net.office.bean.BaseAttr;
import net.office.bean.StaffInfo;
import net.office.bean.WorkTable;
import net.office.service.BaseAttrService;
import net.office.service.LoginService;
import net.office.service.StaffInfoService;
import net.office.service.WorkTableService;
import net.office.utils.TemplateParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.*;

@Controller
public class NoticeController {

    @Autowired
    private BaseAttrService baseAttrService;

    @Autowired
    private WorkTableService workTableService;

    @Autowired
    private StaffInfoService staffInfoService;

    @Autowired
    private LoginService loginService;

    @RequestMapping("/notice/person/list.do")
    public String personList(HttpSession session, Model model){

        StaffInfo staffInfo = (StaffInfo) session.getAttribute("staff");
        List<WorkTable> workTableList = workTableService.findMonthByStaffId(staffInfo.getStaffId(), new Date());
        model.addAttribute("workTableList", workTableList);

        BaseAttr companyNoticeTitleAttr
                = baseAttrService.findByAttrCode(TemplateParam.COMPANY_NOTICE_TITLE_CODE);
        BaseAttr companyNoticeContentAttr
                = baseAttrService.findByAttrCode(TemplateParam.COMPANY_NOTICE_CONTENT_CODE);

        model.addAttribute("companyNoticeTitleAttr", companyNoticeTitleAttr);
        model.addAttribute("companyNoticeContentAttr", companyNoticeContentAttr);

        return "notice";
    }

    @RequestMapping("/notice/all/list.do")
    public String allList(Model model){

        Date search_date = new Date();

        List<StaffInfo> staffInfoList = staffInfoService.findAll();
        List<WorkTable> workTableList = workTableService.findMonthByDate(search_date);

        for(int i=0; i<staffInfoList.size(); i++){

            List<WorkTable> staff_work_list = new ArrayList<WorkTable>();
            for(int j=0; j<workTableList.size(); j++){
                if(staffInfoList.get(i).getStaffId() == workTableList.get(j).getWorkStaffId()){
                    staff_work_list.add(workTableList.get(j));
                }
            }
            staffInfoList.get(i).setWorkTableList(staff_work_list);
        }
        model.addAttribute("staffInfoList", staffInfoList);

        return "notice-edit";
    }

    @RequestMapping("/notice/all/search.do")
    public String allSearch(@RequestParam(value = "staffNo", required = false) String staffNo,
                            @RequestParam(value = "staffName", required = false) String staffName,
                            @RequestParam(value = "year", required = false) Integer year,
                            @RequestParam(value = "month", required = false) Integer month,
                            Model model){

        Date search_date = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(search_date);
        if(year != null && month != null){
            calendar.set(Calendar.YEAR, year);
            calendar.set(Calendar.MONTH, month-1);
        }
        search_date = calendar.getTime();
        model.addAttribute("year", calendar.get(Calendar.YEAR));
        model.addAttribute("month", calendar.get(Calendar.MONTH)+1);

        StaffInfo staffInfo = new StaffInfo();
        if(staffNo != null && staffNo.trim().length() != 0){
            staffInfo.setStaffNo(staffNo);
        }
        if(staffName != null && staffName.trim().length() != 0){
            staffInfo.setStaffName(staffName);
        }

        List<StaffInfo> staffInfoList = staffInfoService.findByStaffInfo(staffInfo);

        for(int i=0; i<staffInfoList.size(); i++){
            StaffInfo staffInfo1 = staffInfoList.get(i);
            staffInfoList.get(i).setWorkTableList(workTableService.findMonthByStaffId(staffInfo1.getStaffId(),search_date));
        }
        model.addAttribute("staffInfoList", staffInfoList);

        return "notice-edit";
    }

    @RequestMapping("/notice/company/update.do")
    @ResponseBody
    public Object companyUpdate(@RequestParam(value = "companyNoticeTitleId") Integer companyNoticeTitleId,
                                @RequestParam(value = "companyNoticeTitle", required = false) String companyNoticeTitle,
                                @RequestParam(value = "companyNoticeContentId") Integer companyNoticeContentId,
                                @RequestParam(value = "companyNoticeContent", required = false) String companyNoticeContent){

        BaseAttr companyNoticeTitleAttr = baseAttrService.findByPrimaryKey(companyNoticeTitleId);
        BaseAttr companyNoticeContentAttr = baseAttrService.findByPrimaryKey(companyNoticeContentId);

        if(companyNoticeTitle != null) {
            companyNoticeTitleAttr.setAttrContent(companyNoticeTitle);
        }else{
            companyNoticeTitleAttr.setAttrContent(null);
        }

        if(companyNoticeContent != null) {
            companyNoticeContentAttr.setAttrContent(companyNoticeContent);
        }else{
            companyNoticeContentAttr.setAttrContent(null);
        }

        int title_res = baseAttrService.updateByPrimaryKey(companyNoticeTitleAttr);
        int content_res = baseAttrService.updateByPrimaryKey(companyNoticeContentAttr);

        Map<String, String> map = new HashMap<String, String>();
        if(title_res > 0 && content_res > 0){
            map.put("res", "1");
        }else{
            map.put("res", "0");
        }
        return map;
    }

    @RequestMapping("/notice/work/update.do")
    @ResponseBody
    public Object workUpdate(@RequestParam(value = "workDate") Date workDate,
                            @RequestParam(value = "workStaffNo") String workStaffNo,
                            @RequestParam(value = "workStaffName") String workStaffName){

        Map<String, String> map = new HashMap<String, String>();

        if(workDate == null){
            map.put("res", "0");
            return map;
        }

        //检查员工是否存在，工号、姓名是否保持一致,...
        StaffInfo staffInfo = loginService.findByStaffNo(workStaffNo);
        if(staffInfo == null){
            map.put("res", "0");
            return map;
        }else if(!staffInfo.getStaffName().equals(workStaffName)){
            map.put("res", "0");
            return map;
        }

        WorkTable workTable = new WorkTable();
        workTable.setWorkDate(workDate);
        workTable.setWorkStaffId(staffInfo.getStaffId());
        workTable.setIsWork("0");

        int res = workTableService.insert(workTable);
        if(res > 0 && res > 0){
            map.put("res", "1");
        }else{
            map.put("res", "0");
        }
        return map;
    }
}
